package com.yitao.cms.service.constant;

import com.skipper.base.service.GenericEntityService;
import com.yitao.client.constant.ConstantDataEnum;
import com.yitao.client.dto.constant.ConstantDataDto;
import com.yitao.client.model.constant.ConstantDataModel;
import com.yitao.client.model.constant.QConstantDataModel;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.RollbackException;

@Service
public class ConstantDataService extends GenericEntityService<ConstantDataModel, ConstantDataDto, QConstantDataModel> {

    public ConstantDataService(EntityManager entityManager) {
        super(entityManager);
    }

    public String getConstantDataModelById(Long id) {
        ConstantDataModel constantDataModel = queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchOne();
        if (constantDataModel == null) {
            throw new RollbackException("未定义的常量参数");
        }
        return queryFactory.select(Q).from(Q).where(Q.id.eq(id)).fetchOne().getConstantValue();
    }

    public Boolean getTongtexpress(Long shopId) {
        String constantDataModelById = getConstantDataModelById(ConstantDataEnum.TONGTEXPRESS.getCode());
        if (constantDataModelById == null) {
            return false;
        }
        return constantDataModelById.contains(shopId.toString());
    }

}